package com.zhida.jiyangcenter.modle.fragment

import android.os.Bundle
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import chooongg.base.annotation.ContentLayout
import chooongg.base.utils.dp2px
import chooongg.base.utils.tipFail
import chooongg.base.utils.toast
import chooongg.base.widget.recyclerView.divider.HorizontalDividerItemDecoration
import chooongg.http.APIException
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yanzhenjie.recyclerview.SwipeMenuItem
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.CollectionListEntity
import com.zhida.jiyangcenter.mian.BaseFragment
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.list.*

@ContentLayout(R.layout.list)
class MyCommentFragment(val type: Int) : BaseFragment() {
    override fun getTitle(): CharSequence {
        return when (type) {
            0 -> "商品"
            1 -> "服务"
            2 -> "文章"
            else -> "未知"
        }
    }
    val adapter=Adapter()
    var page=1

    override fun initConfig(savedInstanceState: Bundle?) {
        recycler_view.setSwipeMenuCreator { leftMenu, rightMenu, position ->
            rightMenu.addMenuItem(SwipeMenuItem(context).apply {
               setBackgroundColorResource(R.color.color_333333)
                setTextColorResource(R.color.colorPrimary)
                text = "删除"
                width = dp2px(70f)
                height = ViewGroup.LayoutParams.MATCH_PARENT
            })
        }
        recycler_view.setOnItemMenuClickListener { menuBridge, adapterPosition ->
            menuBridge.closeMenu()
            adapter.remove(adapterPosition)
            toast("list$adapterPosition")
        }
        recycler_view.layoutManager=LinearLayoutManager(context)
        recycler_view.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(requireContext())
                .sizeResId(R.dimen.dividerSmall).colorResId(R.color.colorBack).build()
        )
        recycler_view.adapter=adapter
        getData()
    }

    private fun getData() {
        lifecycleScope.request({
            APIAccount.service().collectionList(page,type)
        },object :BaseAPI<CollectionListEntity>(){
            override fun onSuccess(t: CollectionListEntity?, state: Int, msg: String) {
                adapter.setNewData(t?.data)
            }

            override fun onError(e: APIException) {
                tipFail(e.eMessage)
            }

        })
    }

    class Adapter:BaseQuickAdapter<CollectionListEntity.Data,BaseViewHolder>(R.layout.item_my_comment_fragment){
        override fun convert(helper: BaseViewHolder, item: CollectionListEntity.Data?) {
            glideImage(helper.getView(R.id.miv_my_comment_fragment),item?.picture?:"")
            helper.setText(R.id.tv_my_comment_name,item?.name)
                .setText(R.id.tv_comment_money,"￥"+item?.price)
        }

    }
}